package shopping.service.impl;

import shopping.dao.OrderDao;
import shopping.domain.Order;
import shopping.domain.OrderItem;
import shopping.domain.vo.OrderItemVo;
import shopping.service.OrderService;
import shopping.transional.Service;
import shopping.transional.Transaction;
import shopping.utils.BeanFactory;

import java.util.ArrayList;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = BeanFactory.newInstance(OrderDao.class);

    /**
     * 保存订单方法
     */
    @Override
    @Transaction
    public void save(Order order, ArrayList<OrderItem> orderItems) {
        //写业务代码
        //保存order订单
        orderDao.saveOrder(order);
        //保存订单项
        for (OrderItem orderItem : orderItems) {
            orderDao.saveOrderItem(orderItem);
        }
    }

    @Override
    public Order findByOidWithItems(String oid) {
        //根据oid查询订单信息
        Order order = orderDao.findByOid(oid);

        //少了该订单相关联的订单项显示信息
        //继续查询该订单的订单项信息
        List<OrderItemVo> vos = orderDao.findItemsVo(oid);
        order.setVos(vos);

        return order;
    }

    @Override
    public List<Order> findOrderByUid(String uid, int pageNumber, int pageSize) {
        //只有订单信息
        List<Order> orders = orderDao.findOrderByUid(uid,pageNumber,pageSize);
        //查询订单项信息
        for (Order order : orders) {
            List<OrderItemVo> itemsVo = orderDao.findItemsVo(order.getOid());
            order.setVos(itemsVo);
        }
        return orders;
    }

    @Override
    public int countByUid(String uid) {
        return orderDao.countByUid(uid);
    }

    @Override
    public void updateSHR(Order order) {
        orderDao.updateSHR(order);
    }

    @Override
    public void updateState(String oid, int orderStateYifukuan) {
        orderDao.updateState(oid,orderStateYifukuan);
    }
}